var util = require('../../utils/util.js');
let app = getApp();
let url = app.url;
let gobalData = app.gobalData;
const moment = require('moment');
let userInfo = wx.getStorageSync('userInfo').userInfo;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id: 0,
		detailsItem: {},
		commentValue: ''
	},
	// 点赞
		clickHelp(e) {
			// 获取文章主键id
			let articleId = this.data.id;
			// 获取当前用户id
			let cid = userInfo.id;
			util(`${url}Article/editCollect`, {
				articleId: articleId,
				cid: cid
			}).then(res => {
				console.log(res);
				if (res.status == 1) {
					this.data.detailsItem.collect = res.content
					this.setData({
						detailsItem: this.data.detailsItem
					})
					wx.showToast({
						title: '点赞成功'
					})
					
				} else {
					wx.showToast({
						title: '点赞失败',
						icon: 'none'
					})
				}
			})
		},
	// 点击进行评论
	bindReply: function(e) {
		// 获取文章主键id
		this.setData({
			releaseFocus: !(this.data.releaseFocus),
		})
	},
	// 获取评论消息
	getInput(e) {
		let comment = e.detail.value;
		this.setData({
			commentValue: comment
		})
	},
	releaseFocus() {
		this.setData({
			releaseFocus: false
		})
	},
	// 发送评论
	commentSend(e) {
		// 获取文章主键id
		let articleId = this.data.id;
		// 获取当前用户id
		let cid = userInfo.id;
		// 获取评论内容
		let commentValue = this.data.commentValue;
		// 请求新增评论接口
		util(`${url}Article/addUserComment`, {
			articleId,
			cid,
			commentValue
		}).then(res => {
			// 添加成功处理
			if (res.status == 1) {
				let name = userInfo.name;
				let avatar = userInfo.avatar;
				let obj = {
					commentValue,
					name,
					avatar
				}
				this.data.detailsItem.comment.unshift(obj);
				this.setData({
					detailsItem: this.data.detailsItem,
					commentValue: ''
				});
			} else {
				wx.showToast({
					title: res.msg,
					icon: 'none'
				})
			}
		})
		// 关闭发送评论input框
		this.setData({
			releaseFocus: !(this.data.releaseFocus)
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.setData({
			id: options.id
		})
		util(`${url}user/getUserArticleDetail`, {
			id: this.data.id
		}).then(res => {
			if (res.status == 1) {
				res.content.createTime = moment(res.content.createTime).fromNow();
				this.setData({
					detailsItem: res.content
				})
			} else {
				wx.showToast({
					title: "获取数据失败！",
					icon: "none"
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage(res) {
		return {
			title: '装笆，您的装修朋友圈',
			path: `pages/decorationRing/index?leaderId=${wx.getStorageSync('userInfo').userInfo.id}`
		}
	},
})
